//
//  SettingController.m
//  Ghac 4S
//
//  Created by apple on 11-12-15.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//
#import "Global.h"
#import "SettingController.h"

@interface SettingController(private)
-(void) showInnerView:(UIView*)innerView;
-(void) hideInnerView;
-(void) innerViewHided:(id)sender;
-(void) exitComplete:(id)sender;
-(void) updateRateLabel;
@end

@implementation SettingController
@synthesize rateView;
@synthesize aboutView;
@synthesize rateLabel;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        // Custom initialization
    }
    return self;
}
-(void)dealloc
{
    [self viewDidUnload];
    [super dealloc];
}
- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}
-(void)backBtnClick:(id)sender
{
    if (m_isInMain)
    {
        id<MainViewSwitchAction> target = [Global mainRootController];
        [target backToEntryView];
    }
    else
    {
        [self hideInnerView];
    }
    
}
-(void)aboutTheApps:(id)sender
{
    [self showInnerView:self.aboutView];
    
}
-(void)rateSetting:(id)sender
{
    [self showInnerView:self.rateView];
}
-(void)exitTheApps:(id)sender
{
    
    UIViewController* mainController = [Global mainRootController];
    [UIView beginAnimations:@"exit" context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:mainController.view cache:YES];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(exitComplete:)];
    [UIView commitAnimations];
    [mainController.view setHidden:YES];
    
}
-(void)showInnerView:(UIView *)innerView
{
    m_isInMain = NO;
    m_currentInnerView = innerView;
    [self.view addSubview:innerView];
    
    CGRect rect = innerView.frame;
    rect.origin.x = rect.size.width;
    [innerView setFrame:rect];
    [UIView beginAnimations:@"innerViewComeIn" context:nil];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self.view cache:YES];
    rect.origin.x = 0;
    [innerView setFrame:rect];
    [UIView commitAnimations];
}
-(void)hideInnerView
{
    m_isInMain = YES;
    CGRect rect = m_currentInnerView.frame;
    [UIView beginAnimations:@"innerViewGetOut" context:nil];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(innerViewHided:)];
    [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self.view cache:YES];
    rect.origin.x = rect.size.width;
    [m_currentInnerView setFrame:rect];
    [UIView commitAnimations];
}
-(void)innerViewHided:(id)sender
{
    [m_currentInnerView removeFromSuperview];
    m_currentInnerView = nil;
}
-(void)exitComplete:(id)sender
{
//    id target = [Global mainRootController];
//    [target release];
    exit(0);
}

-(void)setRateToRightaway:(id)sender
{
    [Global mainInstance].updateTimeDelta = RIGHTAWAY;
    [self hideInnerView];
    [self updateRateLabel];
}
-(void)setRateToDayly:(id)sender
{
    [Global mainInstance].updateTimeDelta = DAYLY;
    [self hideInnerView];
    [self updateRateLabel];
}
-(void)setRateToWeekly:(id)sender
{
    [Global mainInstance].updateTimeDelta = WEEKLY;
    [self hideInnerView];
    [self updateRateLabel];
    
}
-(void)setRateToMonthly:(id)sender
{
    [Global mainInstance].updateTimeDelta = MONTHLY;
    [self hideInnerView];
    [self updateRateLabel];
}
/**
 * to display the rate label on the button;
 *
 *
 *
 */
-(void)updateRateLabel
{
    switch ([Global mainInstance].updateTimeDelta)
    {
        case RIGHTAWAY:
            [self.rateLabel setText:@"马上更新"];
            break;
        case DAYLY:
            [self.rateLabel setText:@"每日"];
            break;
        case WEEKLY:
            [self.rateLabel setText:@"每周"];
            break;
        case MONTHLY:
            [self.rateLabel setText:@"每月"];
            break;
        default:
            break;
    }
}
#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.view setBackgroundColor:[UIColor colorWithWhite:0 alpha:0]];
    m_isInMain = YES;
    [self updateRateLabel];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.rateLabel = nil;
    self.rateView = nil;
    self.aboutView = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
